<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Admin\Category;
use Illuminate\Http\Request;

class CategoryController extends Controller
{
    // 添加栏目页面
    public function add(){
        // 查询一级栏目
        // select * from category where pid =0
        $categoryList = Category::where('pid',0)->get(); 
        return view('admin.category.add', ['category' => $categoryList]);
    
    }

    public function edit($id) {
        $category = Category::find($id);
        $categories = Category::where('pid', 0)->get();
        return view('admin.category.edit', compact('category', 'categories'));
    }
    // 添加栏目保存
    public function save(Request $request){
        $data = $request->all();
        // 验证
        $rule = [
            'name' => 'required|unique:category',
            'pid' => 'required',
            'sort' => 'required|numeric',
        ];
        $message = [
            'name.required' => '栏目名称不能为空',
            'name.unique' => '栏目名称已存在',
            'pid.required' => '上级栏目不能为空',
            'sort.required' => '排序不能为空',
            'sort.numeric' => '排序必须为数字',
        ];
        $request->validate($rule, $message);
        // 
        $re = Category::create($data);
        if ($re) {
            return redirect('category')->with('message', '添加成功');
        } else {
            return redirect('category/add')->with('tip', '添加失败');
        }
        }

    // 查询栏目树形列表页面
    public function index(){
        $data = Category::orderBy('sort','asc')->get()->toArray();
        $tree = $this->treeList($data);
        // dump($tree);
        $category = $this->isLeafNode($tree);
        // dump($category);
        return view('admin.category.index', ['category' => $category]);
    }

    // 返回列表数据
    public function getTreeList(){
        $data = Category::orderBy('sort','asc')->get()->toArray();
        $tree = $this->treeList($data);
        $category = $this->isLeafNode($tree);
        return $category;
    }

    // 将列表数据转成树形数据
    public function treeList($data,$pid=0,&$tree=[],$level=0){
        foreach($data as $v){
            if($v['pid']==$pid){
                $v['level'] = $level;
                $tree[] = $v;
                // 向下查找
                $this->treeList($data,$v['id'],$tree,$level+1);
             }
        }
        return $tree;
    }

    // 判断是否为叶子节点
    public function isLeafNode($tree){
        foreach($tree as $k => $v){
            // 默认值 当前节点是叶子节点
            $tree[$k]['isLeaf'] = true;

            //判断$v是否是叶子节点
            foreach($tree as $vv){
                // 判断$v['id'] 和 $vv['pid'] 
                if($v['id'] == $vv['pid']){
                    // 则这个不是叶子节点
                    $tree[$k]['isLeaf'] = false;
                }
            }
        }
        return $tree;
    }
    public function update(Request $request, $id) {
        $rule = [
            'name' => 'required',
            'pid' => 'required',
        ];
        $message = [
            'name.required' => '栏目名称不能为空',
            'pid.required' => '上级栏目不能为空',
        ];
        $request->validate($rule, $message);
        $category = Category::find($id);
        $category->name = $request->name;
        $category->pid = $request->pid;
        $category->sort = $request->sort;
    
        if ($category->save()) {
            return redirect('category')->with('success', '更新成功');
        } else {
            return back()->with('error', '更新失败');
        }
    }
    
    public function delete(Request $request, $id) {
        $category = Category::find($id);
    
        if ($category->delete()) {
            return redirect('category')->with('success', '删除成功');
        } else {
            return back()->with('error', '删除失败');
        }
    }
}
